/**
 * Copyright (c) 2011 Basil Shikin, VintageRadio Project
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.vintagephone.model.podcaster;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import android.util.Log;

public class Podcaster
{
    private static final String TAG = "Podcaster";
    
    private static final String KEY_URL          = "podcast.url";
    private static final String KEY_UPDATE_HOURS = "podcast.update_hours";
    
    private static final String PROPERTIES_FILE = "podcast.properties";

    private final File m_targetFolder;
    
    private final Properties m_properties;
    private final ScheduledExecutorService m_executor;
    
    public Podcaster( final File targetFolder )
    {
        m_targetFolder = targetFolder;
        
        m_properties = new Properties();
        m_executor = Executors.newScheduledThreadPool( 1 );
    }
    
    public void initialize()
    {
        try
        {
            loadProperties();
            
            final String updateHoursStr = m_properties.getProperty( KEY_UPDATE_HOURS, "3" );
            final int updateHours = Integer.parseInt( updateHoursStr );
            
            final String sourceUrl = m_properties.getProperty( KEY_URL );
            if ( sourceUrl == null ) throw new IOException("no_podcast_url");
            
            final PodcastDownloadTask downloadTask = new PodcastDownloadTask( sourceUrl, m_targetFolder );
            m_executor.scheduleAtFixedRate( downloadTask, updateHours*60*60, updateHours*60*60, TimeUnit.SECONDS );
        } 
        catch (IOException e)
        {
            Log.e(TAG, "Unable to read podcast settins", e );
        }
    }

    private void loadProperties() throws IOException
    {
        InputStream is = null;
        try
        {
            final File propertiesFile = new File( m_targetFolder, PROPERTIES_FILE );
            is = new FileInputStream( propertiesFile );
            
            m_properties.load( is );
        }
        finally
        {
            if ( is != null ) is.close();
        }
    }

}
